//
//  GCMyScene.m
//  GalaxyConqueror
//
//  Created by Test on 8/1/14.
//  Copyright (c) 2014 Stefan.Nestorov. All rights reserved.
//

#import "GCMainScene.h"
#import "GCBackgroundLayer.h"
#import "GCObjectsLayer.h"
#import "GCHUDLayer.h"
#import "JCJoystick.h"
#import "GCPlayerShip.h"
#import "GCEnemyShip.h"

@interface GCMainScene () <SKPhysicsContactDelegate>
// Declare Private Properties here


@end


@implementation GCMainScene
{
    
    BOOL _isRunning;
    CFTimeInterval _lastUpdateTimeInterval;
    
    GCBackgroundLayer* _backgroundLayer;
    GCObjectsLayer* _objectsLayer;
    GCHUDLayer* _HUDLayer;
    JCJoystick* _joystick;
    GCPlayerShip* _playerShip;
    
}

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
        
        self.physicsWorld.contactDelegate = self;
        self.physicsWorld.gravity = CGVectorMake(0.0,-9.8);
        self.physicsWorld.speed = 1.0;
        
        _backgroundLayer = [[GCBackgroundLayer alloc] init];
        [self addChild:_backgroundLayer];
        
        _objectsLayer = [[GCObjectsLayer alloc] init];
        [_objectsLayer setZPosition:_backgroundLayer.zPosition + 1];
        [self addChild:_objectsLayer];
        
        _HUDLayer = [[GCHUDLayer alloc] init];
        [_HUDLayer setZPosition:_objectsLayer.zPosition + 1];
        [self addChild:_HUDLayer];
        
        _playerShip = _objectsLayer.playerShip;
        
        [self initJoystick];
        
        [self performSelector:@selector(startGame) withObject:nil afterDelay:1];
        
    }
    
    return self;
}

- (void)startGame
{
    
    _isRunning = YES;
    
}

-(void)initJoystick
{
    
    _joystick = [[JCJoystick alloc] initWithControlRadius:40 baseRadius:45 baseColor:[SKColor blueColor] joystickRadius:25 joystickColor:[SKColor redColor]];
    [_joystick setPosition:CGPointMake(70,70)];
    _joystick.zPosition = _objectsLayer.zPosition + 1;
    [self addChild:_joystick];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
//    SKNode *node = [self nodeAtPoint:location];
    
    // The fire button pressed
    if ([_HUDLayer.shootButton containsPoint:location]) {
        NSLog(@"Pressed Button !");
        
        [_playerShip shoot];
        
        // uncomment to debug enemy movoment
//        [_objectsLayer addEnemyShip];
    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
    if ( _isRunning == YES ) {
        // Handle time delta.
        // If we drop below 60fps, we still want everything to move the same distance.
        NSTimeInterval deltaTime = currentTime - _lastUpdateTimeInterval;
        _lastUpdateTimeInterval = currentTime;
        
        if (deltaTime > 1) { // more than a second since last update
            deltaTime = 1.0 / 60.0;
            _lastUpdateTimeInterval = currentTime;
        }
        
        [_backgroundLayer update:deltaTime];
        [_playerShip update:deltaTime withJoystickX:_joystick.x andJoystickY:_joystick.y];
        [_objectsLayer update:deltaTime];
    }
    
}


#pragma mark Collision Delegate Method
-(void)didBeginContact:(SKPhysicsContact *)contact
{
    
    // PlayerShip contact with something
    if ( (contact.bodyA.categoryBitMask == PLAYER_SHIP_CATEGORY || contact.bodyB.categoryBitMask == PLAYER_SHIP_CATEGORY)
        && (contact.bodyA.categoryBitMask == ENEMY_SHIP_CATEGORY || contact.bodyB.categoryBitMask == ENEMY_SHIP_CATEGORY)) {
        NSLog(@"Both Ships Colide !");
        
        SKPhysicsBody *enemyShipBody;
        SKPhysicsBody *playerShipBody;
        
        if ( contact.bodyA.categoryBitMask == PLAYER_SHIP_CATEGORY ) {
            playerShipBody = contact.bodyA;
            enemyShipBody = contact.bodyB;
        } else {
            enemyShipBody = contact.bodyA;
            playerShipBody = contact.bodyB;
        }
        [self playerShip:(GCPlayerShip *)playerShipBody.node collideWithEnemyShip:(GCEnemyShip *)enemyShipBody.node];
        return;
    }
    
    if ( (contact.bodyA.categoryBitMask == PLAYER_PROJECTILE_CATEGORY || contact.bodyB.categoryBitMask == PLAYER_PROJECTILE_CATEGORY)
        && (contact.bodyA.categoryBitMask == ENEMY_SHIP_CATEGORY || contact.bodyB.categoryBitMask == ENEMY_SHIP_CATEGORY)) {
        NSLog(@"Player projectile hit EnemyShip !");
        
        SKPhysicsBody *playerProjectileBody;
        SKPhysicsBody *enemyShipBody;
        
        if ( contact.bodyA.categoryBitMask == PLAYER_PROJECTILE_CATEGORY ) {
            playerProjectileBody = contact.bodyA;
            enemyShipBody = contact.bodyB;
        } else {
            enemyShipBody = contact.bodyA;
            playerProjectileBody = contact.bodyB;
        }
        
        [self playerProjectile:(GCProjectile *)playerProjectileBody.node hitEnemyShip:(GCEnemyShip *)enemyShipBody.node];
        return;
    }
    
    if ( (contact.bodyA.categoryBitMask == ENEMY_PROJECTILE_CATEGORY || contact.bodyB.categoryBitMask == ENEMY_PROJECTILE_CATEGORY)
        && (contact.bodyA.categoryBitMask == PLAYER_SHIP_CATEGORY || contact.bodyB.categoryBitMask == PLAYER_SHIP_CATEGORY)) {
        NSLog(@"Enemy projectile hit PlayerShip !");
        
        SKPhysicsBody *enemyProjectile;
        SKPhysicsBody *playerShipBody;
        
        if ( contact.bodyA.categoryBitMask == ENEMY_PROJECTILE_CATEGORY ) {
            enemyProjectile = contact.bodyA;
            playerShipBody = contact.bodyB;
        } else {
            playerShipBody = contact.bodyA;
            enemyProjectile = contact.bodyB;
        }
        
        [self enemyProjectile:(GCProjectile *)enemyProjectile.node hitPlayerShip:(GCPlayerShip *)playerShipBody.node];
        
    }
    
//    NSLog(@"Contact between: %@ and %@",[contact.bodyA.node class], [contact.bodyB.node class]);
    
}

-(void)playerShip:(GCPlayerShip *)playerShip collideWithEnemyShip:(GCEnemyShip *)enemyShip
{
    
    playerShip.health -= 10;
    enemyShip.health -= 10;
    
    NSLog(@"PlayerShip health: %.2f, EnemyShip health: %.2f",playerShip.health, enemyShip.health);
    
}


-(void)playerProjectile:(GCProjectile *)playerProjectile hitEnemyShip:(GCEnemyShip *)enemyShip
{
    
    enemyShip.health -= playerProjectile.damage;
    playerProjectile.shouldBeRemoved = YES;
    NSLog(@"EnemyShip health = %.2f",enemyShip.health);
    
}

-(void)enemyProjectile:(GCProjectile *)enemyProjectile hitPlayerShip:(GCPlayerShip *)playerShip
{
    
    playerShip.health -= enemyProjectile.damage;
    enemyProjectile.shouldBeRemoved = YES;
    NSLog(@"PlayerShip health = %.2f",playerShip.health);
}

@end

































